package fr.obsmip.sedoo.client.activity;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

import fr.obsmip.sedoo.client.ClientFactory;
import fr.obsmip.sedoo.client.ShortcutFactory;
import fr.obsmip.sedoo.client.event.ActionEventConstant;
import fr.obsmip.sedoo.client.message.Message;
import fr.obsmip.sedoo.client.place.DatasetEntryDisplayingPlace;
import fr.obsmip.sedoo.client.place.ExperimentalSiteEntryDisplayingPlace;
import fr.obsmip.sedoo.client.place.ObservatoryEntryDisplayingPlace;
import fr.obsmip.sedoo.client.place.WelcomePlace;
import fr.obsmip.sedoo.client.service.NewsService;
import fr.obsmip.sedoo.client.service.NewsServiceAsync;
import fr.obsmip.sedoo.client.ui.WelcomeView;
import fr.obsmip.sedoo.client.ui.WelcomeView.Presenter;
import fr.obsmip.sedoo.client.ui.misc.Shortcut;
import fr.obsmip.sedoo.shared.domain.MetadataUpdateNewDTO;
import fr.sedoo.commons.client.event.ActionStartEvent;
import fr.sedoo.commons.client.message.CommonMessages;
import fr.sedoo.commons.client.util.LocaleUtil;
import fr.sedoo.commons.client.widget.DialogBoxTools;
import fr.sedoo.commons.shared.domain.New;

public class WelcomeActivity extends RBVPublicActivity implements Presenter  {
	
	private final NewsServiceAsync newsService = GWT.create(NewsService.class);

    public WelcomeActivity(WelcomePlace place, ClientFactory clientFactory) {
        super(clientFactory);
    }

    /**
     * Invoked by the ActivityManager to start a new Activity
     */
    @Override
    public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
    	sendActivityStartEvent();
        final WelcomeView welcomeView = clientFactory.getWelcomeView();
        welcomeView.setPresenter(this);
        containerWidget.setWidget(welcomeView.asWidget());
        broadcastActivityTitle(Message.INSTANCE.welcomeViewHeader());
        List<Shortcut> shortcuts = new ArrayList<Shortcut>();
		shortcuts.add(ShortcutFactory.getWelcomeShortcut());
		clientFactory.getBreadCrumb().refresh(shortcuts);

		
		if (welcomeView.isContentLoaded()== false)
		{
			final ActionStartEvent e = new ActionStartEvent(CommonMessages.INSTANCE.loading(), ActionEventConstant.NEWS_LOADING_EVENT, true);
			clientFactory.getEventBus().fireEvent(e);
			String language = LocaleUtil.getClientLocaleLanguage(clientFactory.getDefaultLanguage());
			newsService.getLatest(language,  clientFactory.getLanguages(), clientFactory.getDisplayLanguages(), new AsyncCallback<ArrayList<New>>() {

				@Override
				public void onSuccess(ArrayList<New> result) {
					clientFactory.getEventBus().fireEvent(e.getEndingEvent());
					List<New> lastNews = new ArrayList<New>();
					List<New> lastUpdates = new ArrayList<New>();
					
					for (New item : result) {
						if (item instanceof MetadataUpdateNewDTO)
						{
							lastUpdates.add(item);
						}
						else
						{
							lastNews.add(item);
						}
					}
					
					welcomeView.setContent(lastNews, lastUpdates);
				}

				@Override
				public void onFailure(Throwable caught) {
					clientFactory.getEventBus().fireEvent(e.getEndingEvent());
					DialogBoxTools.modalAlert(CommonMessages.INSTANCE.error(), CommonMessages.INSTANCE.anErrorHasOccurred()+" : "+caught.getMessage());
				}
			});
		}
    }

	@Override
	public void displayObservatoryEntryMetadata(String uuid) {
		ObservatoryEntryDisplayingPlace place = new ObservatoryEntryDisplayingPlace();
		place.setUuid(uuid);
		clientFactory.getPlaceController().goTo(place);
	}

	@Override
	public void displayExperimentalSiteEntryMetadata(String uuid) {
		ExperimentalSiteEntryDisplayingPlace place = new ExperimentalSiteEntryDisplayingPlace();
		place.setUuid(uuid);
		clientFactory.getPlaceController().goTo(place);		
	}

	@Override
	public void displayDatasetEntryMetadata(String uuid) {
		DatasetEntryDisplayingPlace place = new DatasetEntryDisplayingPlace();
		place.setUuid(uuid);
		clientFactory.getPlaceController().goTo(place);
	}
}